//
//  CustomDatePickerMaskView.swift
//  UI Single
//
//  Created by vivi on 6/3/16.
//  Copyright © 2016 vivi. All rights reserved.
//

import UIKit
import SnapKit
import CoreGraphics

class CustomDatePickerMaskView: UIView {
    
    var gradientLayer1: CAGradientLayer!
    var gradientLayer2: CAGradientLayer!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configureInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configureInit()
    }
    
    func configureInit() {
        
//        gradientLayer1 = CAGradientLayer()
//        gradientLayer1.colors = [UIColor.init(white: 240.0 / 255.0, alpha: 0.7).CGColor, UIColor.init(white: 1.0, alpha: 0.0).CGColor]
//        self.layer.addSublayer(gradientLayer1)
//        
//        gradientLayer2 = CAGradientLayer()
//        gradientLayer2.colors = [UIColor.init(white: 1.0, alpha: 0.0).CGColor, UIColor.init(white: 240.0 / 255.0, alpha: 0.7).CGColor]
//        self.layer.addSublayer(gradientLayer2)
        
        let dimView = UIView()
        dimView.backgroundColor = UIColor.blackColor()
        dimView.alpha = 0.05
        
        self.addSubview(dimView)
        dimView.snp_makeConstraints { make in make.edges.equalTo(self) }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        configureLayer()
    }
    
    func configureLayer() {
//        gradientLayer1.frame = CGRectMake(0, 0, self.frame.width, self.frame.height * 0.5)
//        gradientLayer2.frame = CGRectMake(0.0, self.frame.height * 0.5, self.frame.width, self.frame.height * 0.5)
        
        let maskLayer = CAShapeLayer()
        
        let maskWidth = self.frame.width * 0.869
        let maskHeight:CGFloat = self.frame.height * 0.24
        let maskRect = CGRectMake(self.frame.width / 2.0 - maskWidth / 2.0, self.frame.height / 2.0 - maskHeight / 2.0, maskWidth, maskHeight)
        let rectPath = UIBezierPath(rect: CGRectMake(0.0, 0.0, self.frame.width, self.frame.height))
        let maskPath = UIBezierPath(roundedRect: maskRect, cornerRadius: 20.0)
        rectPath.appendPath(maskPath.bezierPathByReversingPath())
        maskLayer.path = rectPath.CGPath
        
        self.layer.mask = maskLayer
    }
    
    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        return nil
    }
}
